﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;

namespace HSchSchedule.AjaxPages
{
    public partial class AjaxProgram : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(true)]
        public static string GetSubject()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<Subject> lstSubject = DataBase.GetAllSubject(schId);
                    List<JSubject> lstJSubject = new List<JSubject>();
                    foreach (Subject s in lstSubject)
                    {
                        lstJSubject.Add(AssignHelp.SubToJSub(s));
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJSubject);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }

        [WebMethod(true)]
        public static string GetProgram()
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    List<ProgramFull> lstPF = DataBase.GetProgramInfo(schId);
                    List<JProgramFull> lstJPF = new List<JProgramFull>();
                    foreach (ProgramFull pf in lstPF)
                    {
                        JProgramFull jpf = AssignHelp.PFToJPF(pf);
                        lstJPF.Add(jpf);
                    }
                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    return ser.Serialize(lstJPF);
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string DelProgram(int programId)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    DataBase.DeleteProgram(programId);
                    return GUIConstants.SUCCESS_RESULT;
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string AddProgram(JProgram jProgram, List<JProgramDetail> jDetails)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    bool ok = true;
                    ok = ValidateHelp.ValidateProgram(jProgram);
                    if (ok)
                    {
                        foreach (JProgramDetail jpd in jDetails)
                        {
                            ok = ValidateHelp.ValidateProgramDetail(jpd);
                            if (!ok)
                            {
                                break;
                            }
                        }
                    }
                    if (ok)
                    {
                        Program pro = AssignHelp.JProgramToProgram(jProgram);
                        pro.ScheduleInListId = schId;
                        List<ProgramDetail> detail = new List<ProgramDetail>();
                        foreach (JProgramDetail jpd in jDetails)
                        {
                            ProgramDetail pd = AssignHelp.JProgramDetailToProgramDetail(jpd);
                            detail.Add(pd);
                        }
                        DataBase.InsertProgramAndDetail(pro, detail);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }
        [WebMethod(true)]
        public static string UpdateProgram(int currentProgram, JProgram jProgram, List<JProgramDetail> jDetails)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                int schId;
                int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                if (schId > 0)
                {
                    bool ok = true;
                    ok = ValidateHelp.ValidateProgram(jProgram);
                    if (ok)
                    {
                        foreach (JProgramDetail jpd in jDetails)
                        {
                            ok = ValidateHelp.ValidateProgramDetail(jpd);
                            if (!ok)
                            {
                                break;
                            }
                        }
                    }
                    if (ok)
                    {
                        ok = DataBase.CheckProgram(currentProgram);
                    }
                    if (ok)
                    {
                        Program pro = AssignHelp.JProgramToProgram(jProgram);
                        pro.ScheduleInListId = schId;
                        List<ProgramDetail> detail = new List<ProgramDetail>();
                        foreach (JProgramDetail jpd in jDetails)
                        {
                            ProgramDetail pd = AssignHelp.JProgramDetailToProgramDetail(jpd);
                            detail.Add(pd);
                        }
                        //DataBase.InsertProgramAndDetail(pro, detail);
                        DataBase.UpdateProgram(currentProgram, pro, detail);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }
        }

    }

}
